package com.awanp3.nimbus.base.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.awanp3.nimbus.common.idempotent.annotation.RepeatSubmit;
import com.awanp3.nimbus.common.log.annotation.Log;
import com.awanp3.nimbus.common.web.core.BaseController;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.awanp3.nimbus.common.core.domain.R;
import com.awanp3.nimbus.common.core.validate.AddGroup;
import com.awanp3.nimbus.common.core.validate.EditGroup;
import com.awanp3.nimbus.common.log.enums.BusinessType;
import com.awanp3.nimbus.common.excel.utils.ExcelUtil;
import com.awanp3.nimbus.base.domain.vo.BaseLocationVo;
import com.awanp3.nimbus.base.domain.bo.BaseLocationBo;
import com.awanp3.nimbus.base.service.IBaseLocationService;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;

/**
 * 库位信息
 * 前端访问路由地址为:/base/location
 *
 * @author 20014170
 * @date 2025-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/location")
public class BaseLocationController extends BaseController {

    private final IBaseLocationService baseLocationService;

    /**
     * 查询库位信息列表
     */
    @SaCheckPermission("base:location:page")
    @GetMapping("/page")
    public TableDataInfo<BaseLocationVo> page(BaseLocationBo bo, PageQuery pageQuery) {
        return baseLocationService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询库位信息列表
     */
    @SaCheckPermission("base:location:list")
    @GetMapping("/list")
    public R<List<BaseLocationVo>> list(BaseLocationBo bo) {
        return R.ok(baseLocationService.queryList(bo));
    }

    /**
     * 导出库位信息列表
     */
    @SaCheckPermission("base:location:export")
    @Log(title = "库位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseLocationBo bo, HttpServletResponse response) {
        List<BaseLocationVo> list = baseLocationService.queryList(bo);
        ExcelUtil.exportExcel(list, "库位信息", BaseLocationVo.class, response);
    }

    /**
     * 获取库位信息详细信息
     *
     * @param locationId 主键
     */
    @SaCheckPermission("base:location:query")
    @GetMapping("/{locationId}")
    public R<BaseLocationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("locationId") Long locationId) {
        return R.ok(baseLocationService.queryById(locationId));
    }

    /**
     * 新增库位信息
     */
    @SaCheckPermission("base:location:add")
    @Log(title = "库位信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseLocationBo bo) {
        return toAjax(baseLocationService.insertByBo(bo));
    }

    /**
     * 修改库位信息
     */
    @SaCheckPermission("base:location:edit")
    @Log(title = "库位信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseLocationBo bo) {
        return toAjax(baseLocationService.updateByBo(bo));
    }

    /**
     * 删除库位信息
     *
     * @param locationIds 主键串
     */
    @SaCheckPermission("base:location:remove")
    @Log(title = "库位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{locationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("locationIds") Long[] locationIds) {
        return toAjax(baseLocationService.deleteWithValidByIds(List.of(locationIds), true));
    }
}
